package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workbench.domain.WbVisitLogStatistics;
import com.ruoyi.workbench.domain.WbWholeUnitNew;
import com.ruoyi.workbench.service.IWbVisitLogStatisticsService;
import com.ruoyi.workbench.utils.DateEntity;
import com.ruoyi.workbench.utils.Generator;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 上报统计Controller
 *
 * @author hanjiafeng
 * @date 2021-02-26
 */
@Controller
@RequestMapping("/workbench/visit_log_statistics")
public class WbVisitLogStatisticsController extends BaseController {

    private final IWbVisitLogStatisticsService wbVisitLogStatisticsService;

    public WbVisitLogStatisticsController(IWbVisitLogStatisticsService wbVisitLogStatisticsService) {
        this.wbVisitLogStatisticsService = wbVisitLogStatisticsService;
    }

    @RequiresPermissions("workbench:visit_log_statistics:view")
    @GetMapping()
    public String visitLogStatistics() {
        return "workbench/visit_log_statistics/visit_log_statistics" ;
    }

    /**
     * 查询上报统计列表
     */
    @RequiresPermissions("workbench:visit_log_statistics:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest httpServletRequest) {
        WbWholeUnitNew wbWholeUnitNew = Generator.generateWbWholeUnitNew(httpServletRequest);
        String types = httpServletRequest.getParameter("types");
        String[] typeArray = StringUtils.isEmpty(types) ? null : types.split(",");

        DateEntity dateEntity = Generator.initDateUtil(httpServletRequest);
        List<WbVisitLogStatistics> list = wbVisitLogStatisticsService.selectWbVisitLogStatisticsList(wbWholeUnitNew, dateEntity.getPeriod(), dateEntity.getStartTimeStr(), dateEntity.getEndTimeStr(), typeArray);
        return getDataTable(list);
    }

    /**
     * 导出上报统计列表
     */
    @RequiresPermissions("workbench:visit_log_statistics:export")
    @Log(title = "上报统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HttpServletRequest httpServletRequest) {
        WbWholeUnitNew wbWholeUnitNew = Generator.generateWbWholeUnitNew(httpServletRequest);
        String types = httpServletRequest.getParameter("types");
        String[] typeArray = StringUtils.isEmpty(types) ? null : types.split(",");
        DateEntity dateEntity = Generator.initDateUtil(httpServletRequest);
        List<WbVisitLogStatistics> list = wbVisitLogStatisticsService.selectWbVisitLogStatisticsList(wbWholeUnitNew, dateEntity.getPeriod(), dateEntity.getStartTimeStr(), dateEntity.getEndTimeStr(), typeArray);
        ExcelUtil<WbVisitLogStatistics> util = new ExcelUtil<>(WbVisitLogStatistics.class);
        return util.exportExcel(list, "上报统计", ShiroUtils.getLoginName());
    }
}
